

window.onload = function() {
	wxcheckWXSignature();
	window.addEventListener("pageshow", function(){
	    	document.title = "我的成就";
	    setTimeout(function(){},100); 
	}, false);
	window.addEventListener("pagehide", function(){
	    	document.title = "";
	    setTimeout(function(){},100); 
	}, false);
	
	ajaxLoadData();
//	initNavbar();
};

//获取XmlHttpRequest对象
function createXmlHttpRequest(){
    if(window.ActiveXObject){
        return new ActiveXObject("Microsoft.XMLHTTP");
    }else if(window.XMLHttpRequest){
        return new XMLHttpRequest();
    }
}

//加载数据
function ajaxLoadData() {
	var memberId = document.getElementById("memberId").value;

	var xhr = createXmlHttpRequest();
	xhr.onreadystatechange = function() {
		
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				var json = xhr.responseText;
//  						if(json.length>0){
					var jsonData = eval( "(" + json + ")" );//转换后的JSON对象
					a=jsonData;
					init(a);
//  						}
			}
		}
	};
	var url = "/api/ajax/getAchievementData";
	xhr.open("post", url + "?memberId="+memberId+"&mathNum="+Math.random(), true);
	xhr.send();
}

//隐藏块
function hiddenDom(o) {
	document.getElementById(o).style.display = "none";
}
//显示块
function showDom(o) {
	document.getElementById(o).style.display = "block";
}
//取整数 (10.0 = 10)
function getInt(floatNumber) {
	floatNumber = floatNumber % 1 > 0 ? floatNumber : parseInt(floatNumber);
	return floatNumber;
}


var maxStepByDay = 0, maxCaloriesByDay = 0, maxDistanceByDay = 0, stepAVG = 0, caloriesAVG = 0, distanceAVG = 0;

var cumulativeStep = 0;
var jdata;
function init(a) {
	
	document.getElementById("memberId").value=a.achievementRecordList[0].member_id;
	
	document.title = '我的成就';
	
//	createMedals(a);
	
	jdata = a;
	var user_name = a.name;
	if (user_name.length != 0 || user_name == "undefined"){
		document.getElementById("user_name").innerHTML = user_name;
		if(user_name.length>5)
			document.getElementById("user_name").style.width = "95px";
	}
	
	if (a.integral.length != 0 || a.integral == "undefined") {
		document.getElementById("user_integral").innerHTML = a.integral;
		document.getElementById("point").value = a.integral;
	}
	if (a.cumulativeStep.length != 0 || a.cumulativeStep == "undefined"){
		document.getElementById("step").value = a.cumulativeStep;
		cumulativeStep = a.cumulativeStep;
		if (a.cumulativeStep >= 100000) {
			cumulativeStep = getDecimalOne(a.cumulativeStep / 10000);
			document.getElementById("stepUnitText").innerHTML = "万步";
		}
	}
	document.getElementById("cumulativeStep").innerHTML = cumulativeStep;
	if (a.cumulativeCalories.length != 0 || a.cumulativeCalories == "undefined"){
		var c  = getDecimalOne(a.cumulativeCalories);
		c = c.toString();
		c = c.indexOf(".") < 0 ? showCalories(c) : (showCalories(c.substring(0,c.length-2)) + c.substring(c.indexOf("."),c.length));
		document.getElementById("cumulativeCalories").innerHTML = c;
	}
	if (a.cumulativeDistance.length != 0 || a.cumulativeDistance == "undefined") {
		document.getElementById("cumulativeDistance").innerHTML = getDecimalOne(a.cumulativeDistance / 1000);
	}
	if (a.imageUrl.length != 0 || a.imageUrl != "undefined"){
		var headImgUrl=a.imageUrl;
		headImgUrl=headImgUrl.substring(0, (headImgUrl.length-1));
		headImgUrl+="64";
		document.getElementById("user_ico_header").src = "" + headImgUrl + "";
	}
	
	//http://wx.lifesense.com/lifesense-healthcenter/v2/img/level_lv2.png
	var level_img = document.getElementById("user_level_img");
	var url = level_img.src;
	url = url.substring(0, url.length - 5);
	if (!(a.level.length <= 0 || a.level == "undefined")){
		level_img.src = url + a.level + ".png";
	}
	//圆环数据
	if (a.maxStepByDay.length != 0 || a.maxStepByDay == "undefined") {
		maxStepByDay = a.maxStepByDay;
		document.getElementById("step_data").innerHTML = getDecimalOne(maxStepByDay);
		if(maxStepByDay>=100000){
			document.getElementById("step_data").innerHTML = getDecimalOne(maxStepByDay/10000);
			document.getElementById("circle_step").innerHTML = "万步";
		}
	}
	if (a.maxCaloriesByDay.length != 0 || a.maxCaloriesByDay == "undefined") {
		maxCaloriesByDay = a.maxCaloriesByDay;
		document.getElementById("calories_data").innerHTML = getDecimalOne(maxCaloriesByDay);
	}
	if (a.maxDistanceByDay.length != 0 || a.maxDistanceByDay == "undefined") {
		maxDistanceByDay = getDecimalOne(a.maxDistanceByDay/1000); 
		document.getElementById("distance_data").innerHTML = getDecimalOne(maxDistanceByDay);

	}
	if (a.stepAVG.length != 0 || a.stepAVG == "undefined")
		stepAVG = getDecimalOne(a.stepAVG);
	if (a.caloriesAVG.length != 0 || a.caloriesAVG == "undefined")
		caloriesAVG = getDecimalOne(a.caloriesAVG);
	if (a.distanceAVG.length != 0 || a.distanceAVG == "undefined")
		distanceAVG = getDecimalOne(a.distanceAVG/1000);


	
	bindTimeStampList(a);
}
//处理大卡的显示方式  100,000,000.5
function showCalories(c) {
	var n = c.length;
	var s = "";
	for (var i = 0; i < c.length; i++) {
		if (c.length < 3) {
			s = c;
			break;
		} else {
			if (n == 0)
				break;
			if (n == c.length) {
				s = c.substring(n - 3, n);
			} else if (n - 3 > 0) {
				s = c.substring(n - 3, n) + "," + s;
			} else {
				s = c.substring(0, n) + "," + s;
				break;
			}
		}
		n -= 3;
	}
	return s;
}
function levelInfo() {
	var memberId = document.getElementById("memberId").value;
	var openId=document.getElementById("openId").value;
	window.location.href = ctx + "/wechat/pedometerLevelRecord?memberId="
			+ memberId + "&&cumulativeStep=" + jdata.cumulativeStep
			+ "&&integral=" + jdata.integral + "&&level=" + jdata.level+"&&openId="+openId;
}
function integralInfo() {
	var openId=document.getElementById("openId").value;
	window.location.href = ctx + "/v2/pedometer/integral.jsp?openId="+openId;
}
function showMedal() {
	var memberId = document.getElementById("memberId").value;
	var point = document.getElementById("point").value;
	var step = document.getElementById("step").value;
	window.location.href = ctx + "/wechat/badge?point=" + point + "&step="
			+ step + "&memberId=" + memberId;
}

function createMedals(json) {
	var listLength = json.list.length;
	var divs = document.getElementById("medals");
	for (var i = 0; i < listLength; i++) {
		var div = document.createElement("div");
		var src = json.list[i].imageurl;
		var name = json.list[i].name;
		var id = json.list[i].id;
		if ((src.length <= 0 || src == "undefined"))
			src = ctx + "/v2/img/badge.png";
		if ((name.length <= 0 || name == "undefined"))
			name = "3000步";
		div.innerHTML = "<div class='medal_img'><img src='" + ctx + "/v2/img/"
				+ src + "' height='55'></div><div class='medal_name'>" + name
				+ "</div>";
		div.id = id;
		div.className = "medal_ico";
		div.onclick = function() {
			var step = document.getElementById("step").value;
			window.location.href = ctx + "/wechat/badgeDesc?status=1&step="
					+ step + "&id=" + this.id;
		}
		divs.appendChild(div);
	}
	//添加徽章按钮
	// 			var div1 = document.createElement("div");
	// 			div1.innerHTML = "<div id='showMedal' class='medal_ico' onclick='javascript:showMedal()'><div class='medal_img'><img src='${ctx}/v2/img/xbadge.png' height='55'></div></div>"
	// 			divs.appendChild(div1);
};

function changeMAData(cid) {
	if (cid == "max_msg") {
		document.getElementById("max_msg").setAttribute("class", "max_msg");
		document.getElementById("avg_msg").setAttribute("class", "avg_msg");
		if(maxStepByDay>=100000){
			document.getElementById("step_data").innerHTML = getDecimalOne(maxStepByDay/10000);
			document.getElementById("circle_step").innerHTML = "万步";
		}else{
			document.getElementById("step_data").innerHTML = getDecimalOne(maxStepByDay);
			document.getElementById("circle_step").innerHTML = "步";
		}
		document.getElementById("distance_data").innerHTML = getDecimalOne(maxDistanceByDay);
		document.getElementById("calories_data").innerHTML = getDecimalOne(maxCaloriesByDay);
	} else if (cid == "avg_msg") {
		document.getElementById("max_msg").setAttribute("class", "max_msg2");
		document.getElementById("avg_msg").setAttribute("class", "avg_msg2");
		if(stepAVG>=100000){
			document.getElementById("step_data").innerHTML = getDecimalOne(stepAVG/10000);
			document.getElementById("circle_step").innerHTML = "万步";
		}else{
			document.getElementById("step_data").innerHTML = getDecimalOne(stepAVG);
			document.getElementById("circle_step").innerHTML = "步";
		}
		document.getElementById("distance_data").innerHTML = getDecimalOne(distanceAVG);
		document.getElementById("calories_data").innerHTML = getDecimalOne(caloriesAVG);
	}
}

/**
 * 截取保留一位小数  (不四舍五入  小数点后为0取整数)  注：保留两位小数四舍五入之后
 * 
 */
function getDecimalOne(str) {
	var temp = str + "";
	var flag = parseFloat(str);
	if(temp.indexOf(".")>0){
		temp = flag.toFixed(2) + "";
		temp = temp.substring(0, temp.indexOf(".")+2);
	}
//	str = str.indexOf(".") < 0 ? str : (str.toFixed(2)+"").substring(0, str.indexOf(".") + 2);
	return parseFloat(temp);
}
/**
 * 截取保留一位小数
 * @param str
 */
function showDecimalOne(str){
	str = str +"";
	str = str.indexOf(".") < 0 ? str : (str.substring(0, str.indexOf(".") + 2));
	return parseFloat(str);
}

//绑定时间轴数据列表
function bindTimeStampList(data){
	var dataList=data.achievementRecordList;
	
	var timeStampContent_div=document.getElementById("timeStampContent_div");
	
	for(var i=0;i<dataList.length;i++){
		var tsInfo=dataList[i];
		
		var remind=tsInfo.content;
		
		var type=tsInfo.type;
		
		var point=tsInfo.point;
		
		var timeText=tsInfo.created.time;
		
		var time=new Date(timeText);
		timeText=time.format("MM");
		if(timeText>=10){
			timeText=time.format("MM月dd日");
		}
		else{
			timeText=time.format("M月dd日");
		}
		
		
		var ts_Content_div=document.createElement("div");
		ts_Content_div.id="ts_Content_div";
		ts_Content_div.className="tsContent_div";
		
		var ts_timeText_div=document.createElement("div");
		ts_Content_div.id="ts_timeText_div";
		ts_timeText_div.className="ts_timeText_div";
		ts_timeText_div.innerHTML=timeText;
		
		var ts_icon_div=document.createElement("div");
		ts_Content_div.id="ts_icon_div";
		ts_icon_div.className="ts_icon_div";
		
		var ts_Icon_img=document.createElement("img");
		ts_Icon_img.className="ts_Icon_img";
		
		
		var ts_achievementContent_div=document.createElement("div");
		ts_achievementContent_div.id="ts_achievementContent_div";
		ts_achievementContent_div.className="ts_achievementContent_div";
		
		var ts_achievementText_div=document.createElement("div");
		ts_achievementText_div.id="ts_achievementText_div";
		ts_achievementText_div.className="ts_achievementText_div";
		
		var ts_bounsText_div=document.createElement("div");
		ts_bounsText_div.id="ts_bounsText_div";
		ts_bounsText_div.className="ts_bounsText_div";
		
		var ts_connectContent_div=document.createElement("div");
		ts_connectContent_div.id="ts_connectContent_div";
		ts_connectContent_div.className="ts_connectContent_div";
		
		var ts_empty_div=document.createElement("div");
		ts_empty_div.className="ts_empty_div";
		ts_empty_div.innerHTML="&nbsp;";
		
		var ts_connectIcon_div=document.createElement("div");
		ts_connectIcon_div.className="ts_connectIcon_div";
		
		var ts_connect_img=document.createElement("img");
		ts_connect_img.className="ts_connect_img";
		ts_connect_img.id="ts_connect_img";
		ts_connect_img.src="/weixin/v1/img/v3-img/ts_connect.png";
		
		
		timeStampContent_div.appendChild(ts_Content_div);
		ts_Content_div.appendChild(ts_timeText_div);
		ts_Content_div.appendChild(ts_icon_div);
		ts_icon_div.appendChild(ts_Icon_img);
		ts_Content_div.appendChild(ts_achievementContent_div);
		ts_achievementContent_div.appendChild(ts_achievementText_div);
		

		//第一次关注
		if(type==-1){
			ts_achievementText_div.className="ts_achievementText_div";
			ts_achievementText_div.innerHTML=remind;
			ts_Icon_img.src="/weixin/v1/img/v3-img/st.png";
			//ts_achievementContent_div.appendChild(ts_bounsText_div);
		}
		//挑战成功信息
		else if(type==-2){
			ts_achievementContent_div.appendChild(ts_bounsText_div);
			ts_achievementContent_div.className="ts_achievementContent_div";
			
			ts_achievementText_div.innerHTML=remind+"获得奖励"+point+"分。";
			//ts_bounsText_div.innerHTML="获得奖励"+point+"分。";
			ts_Icon_img.src="/weixin/v1/img/v3-img/st2.png";
		}
		//0以上分别对应等级的级别
		else{
			ts_achievementContent_div.appendChild(ts_bounsText_div);
			ts_achievementContent_div.className="ts_achievementContent_div";
			ts_achievementText_div.innerHTML=remind;
			ts_bounsText_div.innerHTML="获得LV"+type+"等级。";
			ts_Icon_img.src="/weixin/v1/img/v3-img/S"+type+".png";
		}
		
		//数据有两条或以上才加上链接的竖线图片
		if(dataList.length>=2){
			//到最后一条数据之后就不再加竖线图片
			if(i==(dataList.length-1)){
				
			}
			else{
				timeStampContent_div.appendChild(ts_connectContent_div);
				ts_connectContent_div.appendChild(ts_empty_div);
				ts_connectContent_div.appendChild(ts_connectIcon_div);
				ts_connectContent_div.appendChild(ts_connect_img);
			}
			//改变第二条或以上的数据的样式
			if(i>=1){
				ts_timeText_div.className="ts_timeText_div ts_margin_2";
				ts_achievementContent_div.className="ts_achievementContent_div ts_noMargin";
				ts_icon_div.className="ts_icon_div ts_noMargin";
			}
			
		}
		
	}
	
	var ts_emptyBottom_div=document.createElement("div");
	ts_emptyBottom_div.className="ts_emptyBottom_div";
	ts_emptyBottom_div.id="ts_emptyBottom_div";
	ts_emptyBottom_div.innerHTML="&nbsp;";
	
	timeStampContent_div.appendChild(ts_emptyBottom_div);
	
}


Date.prototype.format = function(format) {
    /*
     * format="yyyy-MM-dd hh:mm:ss";
     */
    var o = {
        "M+" : this.getMonth() + 1,
        "d+" : this.getDate(),
        "h+" : this.getHours(),
        "m+" : this.getMinutes(),
        "s+" : this.getSeconds(),
        "q+" : Math.floor((this.getMonth() + 3) / 3),
        "S" : this.getMilliseconds()
    };
    if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4- RegExp.$1.length));
        }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)){
            format = format.replace(RegExp.$1, RegExp.$1.length == 1? o[k]:("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
    
};
